package com.hl.controller;

import com.hl.domain.Heartmark;
import com.hl.service.HeartmarkService;
import com.hl.util.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/heartmark")
public class HeartmarkController {
    @Autowired
    private HeartmarkService heartmarkService;
//    上传试卷图片
    @PostMapping("/upload")
    public R upload( MultipartFile  file) throws IOException {
        String path="D:\\picture\\";
        String originFileName = file.getOriginalFilename();
        String ext = originFileName.substring(originFileName.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + ext;
        path+=newFileName;
        file.transferTo(new File(path));
        return R.success(newFileName);
    }
//    下载图片/
    @GetMapping("/downImg")
    public void down(String filename, HttpServletResponse resp) throws IOException {
        String path="D:\\picture\\"+filename;
//        设置响应格式
        resp.setHeader("Conten  t-Disposition","attachement;filename="+filename);
        FileInputStream in=new FileInputStream(path);
        ServletOutputStream out=resp.getOutputStream();
        byte[] b=new byte[1024];
        int i=in.read(b);
        while (i!=-1){
            out.write(b,0,i);
            i=in.read(b);
        }
        out.close();
        in.close();
    }
//    查询所有试卷
    @GetMapping("/selectAll")
    public R selectAll(int pageNum, int pageSize){
        return R.success(heartmarkService.selectAll(pageNum,pageSize));
    }
//    通过试卷名模糊查询
    @GetMapping("/selectByName")
    public R selectByName(String name, @DateTimeFormat(pattern = "yyyy-MM-dd") Date startTime, @DateTimeFormat(pattern = "yyyy-MM-dd") Date endTime){
        return R.success(heartmarkService.selectByName(name,startTime,endTime));
    }
//    联表查询
    @GetMapping("selectAllAnd")
    public R selectAllAnd(Integer id,int pageNum, int pageSize){
        System.out.println("---------------id:" + id);
        return R.success(heartmarkService.selectAllAnd(id,pageNum,pageSize));
    }
//    通过id删除试卷
    @DeleteMapping("/delete")
    public R delete(int id){
        return R.success(heartmarkService.delete(id));
    }
//    修改试卷
    @PutMapping("/update")
    public R update( @RequestBody Heartmark heartmark){
        heartmark.setCreatetime(new Date());
        return R.success(heartmarkService.update(heartmark));
    }
//    添加试卷
    @PostMapping("/insert")
    public R insert(Heartmark heartmark){
        heartmark.setCreatetime(new Date());
        return R.success(heartmarkService.insert(heartmark));
    }
  //返回心理测评打开后的试卷列表
    @GetMapping
    public R getArticle(Integer pageNo,Integer pageSize){
        Map<String, Object> map = heartmarkService.selectAllByPage(pageNo, pageSize);
        return  map!=null?R.success(map):R.fail(null);
    }

    //图片查看功能
    @GetMapping("/download")
    public void download(String filename, HttpServletResponse response) throws IOException {
        FileInputStream inputStream = new FileInputStream("d:\\picture\\" + filename);
        ServletOutputStream outputStream = response.getOutputStream();
        int len=0;
        byte[] b=new byte[1024*8];
        while ((len=inputStream.read(b))!=-1){
            outputStream.write(b,0,len);
        }
        inputStream.close();
        outputStream.close();

    }
}
